<script setup lang="ts">
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPannel from './components/HotPannel.vue'

import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotItemAPI } from '@/services/home'

import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import PageSkeleton from './components/PageSkeleton.vue'
import { useGuessList } from '@/composables'

const bannerList = ref<BannerItem[]>([])
// 获取首页轮播图数据
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  // console.log(res)
  bannerList.value = res.result
}

// 获取首页分类数据
const categoryList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}

// 获取首页热点推荐数据
const hotItemList = ref<HotItem[]>([])
const getHmoeHotItem = async () => {
  const res = await getHomeHotItemAPI()
  hotItemList.value = res.result
}

// 获取猜你喜欢组件实例
const { guessRef, onScrollToLower } = useGuessList()

const isTriggered = ref(false)
// 下拉刷新触发
const onRefresherrefresh = async () => {
  // 开始动画
  isTriggered.value = true
  // await getHomeBannerData(), await getHomeCategoryData(), await getHmoeHotItem()
  // 重置猜你喜欢数据
  guessRef.value?.resetData()
  // 同时发送请求
  await Promise.all([
    // swiperDot.value?.resetDot(),
    getHomeBannerData(),
    getHomeCategoryData(),
    getHmoeHotItem(),
    guessRef.value?.getMore(),
  ])

  // 结束动画
  isTriggered.value = false
}

onLoad(() => {
  getHomeBannerData(), getHomeCategoryData(), getHmoeHotItem()
})
</script>

<template>
  <!-- 自定义导航栏 -->
  <CustomNavbar />
  <!-- 滚动容器 -->
  <scroll-view
    refresher-enabled
    @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggered"
    scroll-y
    class="scroll-view"
    @scrolltolower="onScrollToLower"
  >
    <!-- 骨架屏 -->
    <PageSkeleton v-if="!bannerList.length" />
    <!-- 轮播图 -->
    <XtxSwiper :list="bannerList" />
    <!-- 分类面板 -->
    <CategoryPanel :list="categoryList" />
    <!-- 热点推荐 -->
    <HotPannel :list="hotItemList" />
    <!-- 猜你喜欢 -->
    <XtxGuess ref="guessRef" />
  </scroll-view>
</template>

<style lang="scss">
/* #ifdef APP-PLUS */
// app端滚动容器样式兼容，app端没有page，滚动容器包含在app标签中
#app,
/* #endif */
// 设置小程序页面底色
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}

// 设置滚动容器高度
.scroll-view {
  flex: 1;
}
</style>
